#!/bin/bash
function Usage() {
    echo "Usage : $0"
}

if [[ $# -ge 1 ]];then
    Usage
    exit 1
fi
# 系统时间
NowTime=`date +"%Y-%m-%d %H:%M:%S"`

# 平均负载
LoadAvg=`cut -d " " -f 1-3 /proc/loadavg`

# CPU的温度
CpuTemp=`cat /sys/class/thermal/thermal_zone0/temp`
CpuTemp=`echo "scale=2;${CpuTemp}/1000" | bc`

# CPU的使用率
eval `head -n 1 /proc/stat | awk -v sum1=0 -v idle1=0 \
    '{for(i=2;i<=8;i++) {sum1+=$i} printf("sum1=%d;idle1=%d", sum1, $5)}'` 

sleep 0.5

eval `head -n 1 /proc/stat | awk -v sum2=0 -v idle2=0 \
    '{for(i=2;i<=8;i++) {sum2+=$i} printf("sum2=%d;idle2=%d", sum2, $5)}'`


CpuUsedPerc=`echo "scale=4; (1-(${idle2}-${idle1})/(${sum2}-${sum1}))*100" | bc`

CpuUsedPerc=`printf "%.2f" ${CpuUsedPerc}`

# 报警级别
WarnLevel="normal"

if [[ `echo "${CpuTemp} >= 70" | bc -l` -eq 1 ]];then
    WarnLevel=warning
elif [[ `echo "${CpuTemp} >= 50" | bc -l` -eq 1 ]];then
    WarnLevel=note
fi


echo "$NowTime $LoadAvg $CpuTemp℃ ${CpuUsedPerc}% ${WarnLevel}"


